import { getCategoryAPI } from "@/apis/category";
import { CategoryDataType } from "@/types/category";
import { ref, watchEffect } from "vue";
import { useRoute } from "vue-router";

// Category页面分类业务逻辑
export const useCategory = function () {
    // 获取路由params参数
    const route = useRoute();

    // 获取分类信息以及其下方二级分类信息
    const categoryData = ref<CategoryDataType>();

    // 路由信息变更时获取分类详细信息
    watchEffect(async onCleanup => {
        // 发送请求
        const { response, controller } = getCategoryAPI(route.params.id as string);
        // 设置清理回调方法
        onCleanup(() => controller.abort());
        // 更新分类信息
        categoryData.value = (await response).data.result;
    });

    return { categoryData };
};